package com.zw.cc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;

/**
 * Created by zhaowuz on 2018/1/8
 */
public class LearnOptional {

    private static final Logger LOGGER = LoggerFactory.getLogger(LearnOptional.class);

    public static void JDKOptional() {
        Optional<Integer> i = Optional.empty();
//        System.out.println(i.get()); // throw new NoSuchElementException("No value present");
        LOGGER.info("{}",i.isPresent()); //是否有值

//        Optional<Integer> notNullOptOfNull = Optional.of(null); //传入非空值 否则NullPointerException
        Optional<Integer> notNullOpt = Optional.of(1); //传入非空值 否则NUllException
        LOGGER.info("{}",notNullOpt.get()); // 1
        LOGGER.info("{}",notNullOpt.isPresent()); //是否有值
        notNullOpt.ifPresent(value->{
            LOGGER.info("{}",value);
        });
        i.ifPresent(value->{  //如果值不为空则执行函数体
            LOGGER.info("null ----- {}",value);
        });
        LOGGER.info("null ----- filter method");
        i.filter(value -> {
            LOGGER.info("{}",value);
            return true;
        });
        LOGGER.info("notNullOpt ----- filter method");
        Optional filter= notNullOpt.filter(value -> {
            LOGGER.info("{}", value);
            return false;
        });
        LOGGER.info("{}",notNullOpt.isPresent());
        LOGGER.info("{}",filter.isPresent());
//        LOGGER.info("notNullOpt ----- filter method if return false ", filter.get());  NullPointerException
        Optional<Integer> mapOpt = notNullOpt.map(value -> new Integer(2));
        LOGGER.info("mapOpt --{}", mapOpt.isPresent());
        mapOpt.ifPresent(v-> LOGGER.info("mapOpt -- value {}",v));


    }



    public static void main(String[] args) {
        JDKOptional();
    }
}
